<?php

namespace App\Http\Controllers\lxSystem;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;


class FileController extends Controller
{
    function upload(Request $request)
    {
        if ($request->hasFile('Filedata') and $request->file('Filedata')->isValid()) {

            $result = array();

            //文件类型
            $allow = array('image/jpeg', 'image/png', 'image/gif');

            $mine = $request->Filedata->getMimeType();
            if (!in_array($mine, $allow)) {
                $result['status'] = 0;
                $result['info'] = '文件类型错误，只能上传图片';
                return $result;
            }

            //文件大小判断$filePath
            $max_size = 1024 * 1024 * 3;
            $size = $request->Filedata->getClientSize();
            if ($size > $max_size) {
                $result['status'] = 0;
                $result['info'] = '文件大小不能超过3M';
                return $result;
            }

            //返回新文件名
            $path = $request->Filedata->store('images');
            //$path=images/filename
            //绝对路径
            $filePath = storage_path('app/') . $path;
            // \Debugbar::info(storage_path());
            //保存到七牛
            list($ret, $err) = qiniu_upload($filePath);

            if ($err !== null) {
                $result['status'] = 0;
                $result['info'] = '无法储存到服务器';
                return $result;
            }

            $result['status'] = 1;
            $result['info'] = $ret['key'];
            return $result;
        }
    }

}
